import time
from dataclasses import dataclass


class Msg(object):
    def __init__(self, toUserName : str, fromUserName : str):
        self.param_dict = dict()
        self.param_dict['CreateTime'] = int(time.time())
        self.param_dict['ToUserName'] = toUserName
        self.param_dict['FromUserName'] = fromUserName


    def BuildMsg(self) -> str:
        pass


class TextMsg(Msg):
    def __init__(self, toUserName : str, fromUserName : str, content : str):
        super(TextMsg, self).__init__(toUserName, fromUserName)
        self.param_dict['Content'] = content


    def BuildMsg(self) -> str:
        xml_form = """
            <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[{Content}]]></Content>
            </xml>
            """
        return xml_form.format(**self.param_dict)


class ImageMsg(Msg):
    def __init__(self, toUserName : str, fromUserName : str, mediaId : str):
        super(ImageMsg, self).__init__(toUserName, fromUserName)
        self.param_dict['MediaId'] = mediaId


    def BuildMsg(self) -> str:
        xml_form = """
            <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[image]]></MsgType>
                <Image>
                <MediaId><![CDATA[{MediaId}]]></MediaId>
                </Image>
            </xml>
            """
        return xml_form.format(**self.param_dict)


@dataclass
class ArticleParameters():
    title : str
    title : str
    description : str
    picUrl : str
    url : str


class ArticleMsg(Msg):
    def __init__(
        self, 
        toUserName: str, 
        fromUserName: str,
        articleList : list[ArticleParameters]
    ):
        super(ArticleMsg, self).__init__(toUserName, fromUserName)
        self.param_dict['numberOfArticles'] = len(articleList)
        self.articleList = articleList


    def BuildMsg(self) -> str:
        xml_form_head = """
            <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[news]]></MsgType>
                <ArticleCount>{numberOfArticles}</ArticleCount>
                <Articles>

        """.format(**self.param_dict)

        xml_form_foot = """
                </Articles>
            </xml>
        """

        xml_form_item_list = ''
        for article in self.articleList:
            xml_form_item_list += """
                    <item>
                        <Title><![CDATA[{}]]></Title>
                        <Description><![CDATA[{}]]></Description>
                        <PicUrl><![CDATA[{}]]></PicUrl>
                        <Url><![CDATA[{}]]></Url>
                    </item>
            """.format(
                article.title,
                article.description,
                article.picUrl,
                article.url,
            )

        return xml_form_head + xml_form_item_list + xml_form_foot